"""
@author: xxw
@file: 2023-4-16-猜拳游戏.py
@time: 2023/4/16 17:40
"""
# 用户可以创建房间，加入房间
# 加入房间之后，房间里面要等于两个人
# 两个人互相猜拳，按任意键，A，B随机出剪刀石头布，输的人扣100金币
# 直到金豆输完了，或者按q退出

# 房间类
# 属性
    # 房间id
    # 房间里面的人  -- []
# 方法
    #进入房间
    #退出房间

# 用户类
# 属性
    #用户id
    #金豆
    #属于哪个房间
# 方法
    #猜拳

import random

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = [None, None]

    def enter_room(self, player_id):
        if None in self.players:
            index = self.players.index(None)
            self.players[index] = player_id
            return True
        else:
            return False

    def exit_room(self, player_id):
        if player_id in self.players:
            index = self.players.index(player_id)
            self.players[index] = None
            return True
        else:
            return False

class Player:
    def __init__(self, player_id, gold=500):
        self.player_id = player_id
        self.gold = gold
        self.room_id = None

    def join_room(self, room_id):
        if self.room_id is None:
            room = rooms.get(room_id)
            if room is not None and room.enter_room(self.player_id):
                self.room_id = room_id
                return True
        return False

    def leave_room(self):
        if self.room_id is not None:
            room = rooms.get(self.room_id)
            if room is not None and room.exit_room(self.player_id):
                self.room_id = None
                return True
        return False

    def guess(self):
        return random.randint(1, 3)  # 随机出拳，1代表石头，2代表剪刀，3代表布

rooms = {}
for i in range(1, 6):
    rooms[i] = Room(i)

players = {}
for i in range(1, 11):
    players[i] = Player(i)

while True:

    choice = input("1. 创建房间\n2. 加入房间\n3. 离开房间\n4. 开始游戏\n请选择你的操作(按q退出)：")

    if choice == "1":
        room_id = int(input("请输入房间号："))
        if rooms.get(room_id) is None:
            rooms[room_id] = Room(room_id)
            print("创建房间成功！")
        else:
            print("房间已存在！")

    elif choice == "2":
        player_id = int(input("请输入玩家号："))
        room_id = int(input("请输入房间号："))
        if players.get(player_id) is None:
            print("玩家不存在！")
        elif rooms.get(room_id) is None:
            print("房间不存在！")
        elif players[player_id].join_room(room_id):
            print("加入房间成功！")
        else:
            print("加入房间失败！")

    elif choice == "3":
        player_id = int(input("请输入玩家号："))
        if players.get(player_id) is None:
            print("玩家不存在！")
        elif players[player_id].leave_room():
            print("离开房间成功！")
        else:
            print("离开房间失败！")

    elif choice == "4":
        room_id = int(input("请输入房间号："))
        room = rooms.get(room_id)
        if room is None:
            print("房间不存在！")
        elif None in room.players:
            print("房间人数不足！")
        else:
            player1_id, player2_id = room.players
            player1 = players[player1_id]
            player2 = players[player2_id]
            while True:
                input("按任意键开始游戏：")
                guess1 = player1.guess()
                guess2 = player2.guess()
                print("玩家{}出拳：{}".format(player1_id, guess1))
                print("玩家{}出拳：{}".format(player2_id, guess2))
                if guess1 == guess2:
                    print("平局！")
                elif (guess1 == 1 and guess2 == 2) or (guess1 == 2 and guess2 == 3) or (guess1 == 3 and guess2 == 1):  # 玩家1胜利
                    print("玩家{}胜利！".format(player1_id))
                    player2.gold -= 100
                    if player2.gold <= 0:
                        print("玩家{}金豆输光了，游戏结束！".format(player2_id))
                        player1.gold += player2.gold
                        player2.gold = 0
                        players[player2_id].leave_room()
                        break
                else:
                    print("玩家{}胜利！".format(player2_id))
                    player1.gold -= 100
                    if player1.gold <= 0:
                        print("玩家{}金豆输光了，游戏结束！".format(player1_id))
                        player2.gold += player1.gold
                        player1.gold = 0
                        players[player1_id].leave_room()
                        break

    elif choice == "q":
        break

    else:
        print("请输入正确的操作！")



